/*
 *  UIBase.cpp
 *
 *	Date: 2011-4-23
 *  Author: linshenqi
 */

#include "stdafx.h"
#include "./UIBase.h"

GraphicBase::GraphicBase(const CRect *pRect,
					     const GRAPHICPROP *pProp,
					     const char *bmproute)
: Renderer()
, m_pRect(NULL)
, m_pProp(NULL)
, m_pParent(NULL)
{
	if(pRect)
	{
		m_pRect = new CRect(*pRect);
	}

	if(m_pRect && pProp)
	{
		m_pProp = new GRAPHICPROP;
		memset(m_pProp, 0, sizeof(GRAPHICPROP));
		memcpy(m_pProp, pProp, sizeof(GRAPHICPROP));
	}

	if(bmproute && m_pRect)
	{
		HBITMAP hBmp = (HBITMAP)::LoadImage(::AfxGetInstanceHandle(),
										    bmproute,
										    IMAGE_BITMAP,
										    m_pRect->Width(),
										    m_pRect->Height(),
										    LR_LOADFROMFILE);
		m_pBmp.DeleteObject();
		m_pBmp.Attach(hBmp);
	}
}

GraphicBase::~GraphicBase()
{
	if(m_pRect)
	{
		delete m_pRect;
		m_pRect = NULL;
	}

	if(m_pProp)
	{
		delete m_pProp;
		m_pProp = NULL;
	}
}

void GraphicBase::SetBmp(const char *bmproute, const CRect *pRect)
{
	CRect rc;
	if(!pRect)
	{
		if(!m_pRect)
			return;
		rc = *m_pRect;
	}
	else
	{
		rc = *pRect;
	}

	HBITMAP hBmp = (HBITMAP)::LoadImage(::AfxGetInstanceHandle(),
									    bmproute,
									    IMAGE_BITMAP,
									    rc.Width(),
									    rc.Height(),
									    LR_LOADFROMFILE);


	m_pBmp.DeleteObject();
	m_pBmp.Attach(hBmp);
}

void GraphicBase::Offset(const CPoint *point)
{
	if(!point || !m_pRect)
	{
		return;
	}

	int ox = point->x - m_pRect->CenterPoint().x;
	int oy = point->y - m_pRect->CenterPoint().y;

	m_pRect->left += ox;
	m_pRect->right += ox;
	m_pRect->top += oy;
	m_pRect->bottom += oy;
}

void GraphicBase::SetRect(const CRect *pRect)
{
	if(!m_pRect)
	{
		m_pRect = new CRect;
	}
	m_pRect->SetRect(pRect->left, pRect->top, pRect->right, pRect->bottom);
}

void GraphicBase::SetProp(const GRAPHICPROP *pProp)
{
	if(!m_pProp)
	{
		m_pProp = new GRAPHICPROP;
	}

	memset(m_pProp, 0, sizeof(GRAPHICPROP));
	memcpy(m_pProp, pProp, sizeof(GRAPHICPROP));
}

void GraphicBase::SetParentUI(GraphicBase *pParent)
{
	this->m_pParent = pParent;
}

bool GraphicBase::OnPaint(CPaintDC *pDC, CDC *pMemDC)
{
	Renderer::OnPaint();
	return true;
}

bool GraphicBase::OnMouseMove(UINT nFlags, CPoint point)
{
	Renderer::OnMouseMove(nFlags, point);
	return true;
}

bool GraphicBase::OnLButtonDown(UINT nFlags, CPoint point)
{
	Renderer::OnLButtonDown(nFlags, point);
	return true;
}

bool GraphicBase::OnLButtonUp(UINT nFlags, CPoint point)
{
	Renderer::OnMouseMove(nFlags, point);
	return true;
}

bool GraphicBase::OnRButtonDown(UINT nFlags, CPoint point)
{
	Renderer::OnRButtonDown(nFlags, point);
	return true;
}

bool GraphicBase::OnRButtonUp(UINT nFlags, CPoint point)
{
	Renderer::OnRButtonUp(nFlags, point);
	return true;
}

bool GraphicBase::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	Renderer::OnLButtonDblClk(nFlags, point);
	return true;
}


void GraphicBase::SetBaseWindow(CWnd *pWnd)
{
	Renderer::SetBaseWindow(pWnd);
}